﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmTHPhieuNhapDC : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        string NhaCungCap = "";
        string TuNgay = "";
        string TrangThaiThanhToan = "";
        string TinhTrangNhapKho = "";
        private MatHangBOCollection matHangBOCollection = null;
        private PhieuNhapDCBOCollection PhieuNhapDCBOCollection = PhieuNhapDCBOCollection.NewPhieuNhapDCBOCollection();
        private PhieuNhapDCChiTietBOCollection PhieuNhapDCChiTietBOCollection = PhieuNhapDCChiTietBOCollection.NewPhieuNhapDCChiTietBOCollection();
        private PhieuNhapDCBO PhieuNhapDCBO = PhieuNhapDCBO.NewRootPhieuNhapDCBO();

        #endregion variant

        //=====================================
        #region form
        public FrmTHPhieuNhapDC()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmTHPhieuNhapDC_Load);
            this.FormClosing += new FormClosingEventHandler(FrmTHPhieuNhapDC_FormClosing);
            this.btnHuy.Click += new EventHandler(btnHuy_Click);
            this.btnTimKiem.Click += new EventHandler(btnTimKiem_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.grdViewMatHang.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdViewMatHang.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmTHPhieuNhapDC_Load(object sender, EventArgs e)
        {
            RefreshData();
            //SetFormStatus(this.formStatus);

            Utility.InitControl(this.reLookUpMatHangID);
            Utility.InitControl(this.reLookUpDonViTinhID);

            Utility.InitControl(this.lookUpEditNhaCungCap);

            Utility.InitControl(this.reLookUpNhaCungCapID);


        }

        private void FrmTHPhieuNhapDC_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }

        #endregion form

        //=====================================
        #region control

        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion button

        //=====================================
        #region grid

        private void grdView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterMatHang();
        }

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewMatHang.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }

        #endregion grid

        //=====================================
        #region function
        private void FilterMatHang()
        {
            //Session.NhomHangID = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("NhomHangID"));

            //if (Session.NhomHangID == -1)
            //{
            //    //this.grdViewPhieuNhapDCChiTiet.ActiveFilterString = "MatHangID=" + int.MaxValue;

            //    this.giaBOCollection = GiaBOCollection.GetGiaBOCollection();
            //    this.bdSourceGia.DataSource = this.giaBOCollection.Clone();
            //}
            //else
            //{
            //    MatHangBOCollection matHangBOCollection = MatHangBOCollection.GetMatHangBOCollection("NhomHangID = " + Session.NhomHangID.ToString());
            //    string strWhere = "";
            //    string listID = "";
            //    int recordCount = matHangBOCollection.Count;
            //    if (recordCount > 0)
            //    {
            //        for (int i = 0; i < recordCount; i++)
            //        {
            //            object _idVattu = matHangBOCollection[i].MatHangID;
            //            listID += _idVattu.ToString() + ",";
            //        }
            //    }
            //    strWhere = strWhere + " MatHangID in ('" + listID.Replace(",", "','") + "')";
            //    this.giaBOCollection = GiaBOCollection.GetGiaBOCollection(strWhere);
            //    this.bdSourceGia.DataSource = this.giaBOCollection.Clone();
            //}
            //this.grdViewPhieuNhapDCChiTiet.CheckLoaded();
            //SetSttPhieuNhapDCChiTiet();
        }
        private void ResetData()
        {
            lookUpEditNhaCungCap.EditValue = null;
            this.PhieuNhapDCBO = Model.PhieuNhapDCBO.NewRootPhieuNhapDCBO();
            this.PhieuNhapDCChiTietBOCollection = Model.PhieuNhapDCChiTietBOCollection.NewPhieuNhapDCChiTietBOCollection();
        }

        private void RefreshData()
        {
            ResetData();
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinhID, donViTinhBOList, donViTinhBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reLookUpMatHangID, matHangBOList, matHangBOList.Count);

            KhachHangBOList NhaCungCapBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.reLookUpNhaCungCapID, NhaCungCapBOList, NhaCungCapBOList.Count);
            Utility.SetData(this.lookUpEditNhaCungCap, NhaCungCapBOList, NhaCungCapBOList.Count);

            dateEditTungay.DateTime = dateEditDenngay.DateTime = DateTime.Now;
            #endregion BOList
            this.PhieuNhapDCBOCollection = Model.PhieuNhapDCBOCollection.NewPhieuNhapDCBOCollection();

            this.bdSourcePhieuNhapDC.DataSource = this.PhieuNhapDCBOCollection.Clone();
            this.matHangBOCollection = MatHangBOCollection.GetMatHangBOCollection();
            this.bdSourceMatHang.DataSource = this.matHangBOCollection.Clone();
            FilterMatHang();
        }

        #endregion function


        private void btnTimKiem_Click(object sender, EventArgs e)
        {

            string strWhere = "PhieuNhapDCID >= 0";
            if (checkCongNo.Checked)
                if (checkThanhToan.Checked)
                {
                    strWhere = strWhere + "and ThanhToan = 1";
                    TrangThaiThanhToan = "Trạng thái Hóa đơn: Đã Thanh toán (thanh toán đủ,công nợ)";
                }
                else
                {
                    strWhere = strWhere + "and ThanhToan = 1 and TinhTrangThanhToan < 0";
                    TrangThaiThanhToan = "Trạng thái Hóa đơn: Hóa đơn công nợ";
                }
            else
                if (checkThanhToan.Checked)
                {
                    strWhere = strWhere + "and ThanhToan = 1 and TinhTrangThanhToan > 0";
                    TrangThaiThanhToan = "Trạng thái Hóa đơn: Hóa đơn đã thanh toán đủ";
                }
                else
                {
                    strWhere = strWhere + "and ThanhToan = 0 and TinhTrangThanhToan = 0";
                    TrangThaiThanhToan = "Trạng thái Hóa đơn: Tất cả các hóa đơn (Chưa thanh toán, thanh toán đủ, công nợ)";
                }

            if (checkNgay.Checked)
            {
                strWhere = strWhere + " and (convert(nvarchar,NgayLapPhieu,111) >= '" + dateEditTungay.DateTime.ToString("yyyy/MM/dd") + "') and (convert(nvarchar,NgayLapPhieu,111) <= '" + dateEditDenngay.DateTime.ToString("yyyy/MM/dd") + "')";
                TuNgay = "Ngày lập Hóa đơn: Từ ngày " + dateEditTungay.DateTime.ToString("dd/MM/yyyy") + " Đến ngày " + dateEditDenngay.DateTime.ToString("dd/MM/yyyy");
            }
            else
                TuNgay = "Ngày lập Hóa đơn: Tất cả các ngày ";


            if (checkNhapKho.Checked)
            {
                strWhere = strWhere + "and TinhTrangXuatKho = 1 ";
                TuNgay = "Tình trạng nhập kho: Đã nhập vào kho";
            }
            else
            {
                strWhere = strWhere + "and TinhTrangXuatKho = 0 ";
                TuNgay = "Tình trạng nhập kho: Phiếu nhập đang xử lý, Chưa nhập vào kho";
            }


            if (checkNhaCungCap.Checked)
                if (lookUpEditNhaCungCap.EditValue != null)
                {
                    strWhere = strWhere + " and NhaCungCapID = " + lookUpEditNhaCungCap.EditValue.ToString();
                    NhaCungCap = "Nhà cung cấp: " + lookUpEditNhaCungCap.Text.ToString();
                }
                else
                    NhaCungCap = "Nhà cung cấp: Tất cả Nhà cung cấp";
            else
                NhaCungCap = "Nhà cung cấp: Tất cả Nhà cung cấp";

            timKiem(strWhere);
        }


        private void timKiem(string strwhere)
        {

            PhieuNhapDCBOCollection = Model.PhieuNhapDCBOCollection.GetPhieuNhapDCBOCollection(strwhere, "PhieuNhapDCID", "DESC");
            this.bdSourcePhieuNhapDC.DataSource = this.PhieuNhapDCBOCollection.Clone();

            //tinhtien();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            RPhieuNhapDCBOCollection rPhieuNhapDCBOColecction = RPhieuNhapDCBOCollection.NewRPhieuNhapDCBOCollection();
            int rowcount = PhieuNhapDCBOCollection.Count;
            if (rowcount > 0)
                for (int i = 0; i < rowcount; i++)
                {
                    RPhieuNhapDCBO rPhieuNhapDCBO = RPhieuNhapDCBO.NewRootRPhieuNhapDCBO();
                    rPhieuNhapDCBO.CopyFromRPhieuNhapDCBO(PhieuNhapDCBOCollection[i]);
                    rPhieuNhapDCBO.STT0 = i + 1;
                    rPhieuNhapDCBOColecction.Add(rPhieuNhapDCBO);
                }

            Report.rpTongHopPhieuNhapDC rTongHopPhieuNhapDC = new TanHungLong.Report.rpTongHopPhieuNhapDC(TinhTrangNhapKho, TuNgay, NhaCungCap, TrangThaiThanhToan, rPhieuNhapDCBOColecction);
            rTongHopPhieuNhapDC.ShowPreviewDialog();
        }

        private void btnInChiTiet_Click(object sender, EventArgs e)
        {

            int PhieuNhapDCID = StaticUtil.GetInt(gridViewPhieuNhapDC.GetFocusedRowCellValue("PhieuNhapDCID"), -1);
            if (PhieuNhapDCID >= 0)
            {
                this.PhieuNhapDCBO = Model.PhieuNhapDCBO.GetRootPhieuNhapDCBO(PhieuNhapDCID);
                this.PhieuNhapDCChiTietBOCollection = Model.PhieuNhapDCChiTietBOCollection.GetPhieuNhapDCChiTietBOCollection("PhieuNhapDCID = " + PhieuNhapDCID.ToString());

                RPhieuNhapDCBO rPhieuNhapDCBO = RPhieuNhapDCBO.NewRootRPhieuNhapDCBO();
                rPhieuNhapDCBO.CopyFromRPhieuNhapDCBO(PhieuNhapDCBO);
                Report.rpPhieuNhapDCKho rPhieuNhapDCHang = new TanHungLong.Report.rpPhieuNhapDCKho(rPhieuNhapDCBO, PhieuNhapDCChiTietBOCollection);
                rPhieuNhapDCHang.ShowPreviewDialog();
            }
            else
                MessageBox.Show("Chưa chọn Hóa đơn để in??", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}